using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Manaddiction
{
    [System.Serializable]
    public class UserData
    {
        public string username;
        public int coins;
        public UserCardData[] cards;
        public UserDeckData deck;
        
        public UserData()
        {
            cards = new UserCardData[0];
            deck = new UserDeckData();
        }
        
        // 更新用户卡组
        public void UpdateDeck(UserDeckData newDeck)
        {
            deck = newDeck;
        }

        
        public UserCardData GetCard(string tid)
        {
            foreach (UserCardData card in cards)
            {
                if (card.tid == tid)
                    return card;
            }
            return null;
        }

        public int GetCardQuantity(string tid)
        {
            if (cards == null)
                return 0;

            foreach (UserCardData card in cards)
            {
                if (card.tid == tid)
                    return card.quantity;
            }
            return 0;
        }
        
        public void AddCard(string tid, int quantity)
        {
            bool found = false;
            foreach (UserCardData card in cards)
            {
                if (card.tid == tid)
                {
                    found = true;
                    card.quantity += quantity;
                }
            }
            if (!found)
            {
                UserCardData npack = new UserCardData();
                npack.tid = tid;
                npack.quantity = quantity;
                List<UserCardData> acards = new List<UserCardData>(cards);
                acards.Add(npack);
                cards = acards.ToArray();
            }
        }
        
        public bool HasCard(string card_tid, int quantity = 1)
        {
            foreach (UserCardData card in cards)
            {
                if (card.tid == card_tid && card.quantity >= quantity)
                    return true;
            }
            return false;
        }

        public void AddCoin(int quantity)
        {
            coins += quantity;
        }
        
        public string GetCoinsString()
        {
            return coins.ToString();
        }
    }
    
    [System.Serializable]
    public class UserCardData
    {
        public string tid;
        public int quantity;

        public static string GetTid(string card_id)
        {
            return card_id;
        }
        
        public static CardData GetCardData(string tid)
        {
            return GameDataManager.GetCard(tid);
        }

        public static string GetCardId(string tid)
        {
            return tid;
        }
    }
    
    [System.Serializable]
    public class UserDeckData
    {
        public List<CardDataTmp> deck_cards;

        public UserDeckData()
        {
            deck_cards = new List<CardDataTmp>(); // 确保永远不为null
        }
        
        public int GetQuantity()
        {
            int sum = 0;
            foreach (CardDataTmp qcard in deck_cards)
            {
                sum += qcard.quantity;
            }
            return sum;
        }
    }

    public class CardDataTmp
    {
        public string card_name;
        public int quantity;
    }
}
